package com.example.gx.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;

import java.util.List;

public class LocActivity extends AppCompatActivity {
    public cLocInfo curLocInfo;
    public TextView txtLoc;
    private LocationClient myLocationClient;
    private LocationClientOption myLocoption;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loc);

        curLocInfo = new cLocInfo();//初始化
        txtLoc = (TextView) findViewById(R.id.txtLocInfo);
        myLocationClient = new LocationClient(this);
        initLocation();
        /* 本地取址Client 端设置 Option选项 */
        myLocationClient.setLocOption(myLocoption);
        /* 设置监听器，监听服务器发送过来的地址信息 */
        myLocationClient.registerLocationListener(new MyLocLsn());
        myLocationClient.start();
    }
    //获取位置的参数初始化
    private void initLocation(){
        myLocoption = new LocationClientOption();
        myLocoption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        myLocoption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span=1000;
        myLocoption.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        myLocoption.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        myLocoption.setOpenGps(true);//可选，默认false,设置是否使用gps
        myLocoption.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        myLocoption.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        myLocoption.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        myLocoption.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        myLocoption.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        myLocoption.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        myLocationClient.setLocOption(myLocoption);
    };
    //地址信息监听器
    private class MyLocLsn implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //Receive Location
            curLocInfo.set_time(location.getTime());
            curLocInfo.set_code(location.getLocType());
            curLocInfo.set_latitude(location.getLatitude());
            curLocInfo.set_lontitude(location.getLongitude());
            curLocInfo.set_radius(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
                curLocInfo.set_speed(location.getSpeed());
                curLocInfo.set_satellite(location.getSatelliteNumber());
                curLocInfo.set_height(location.getAltitude());
                curLocInfo.set_direction(location.getDirection());
                curLocInfo.set_addr(location.getAddrStr());
                curLocInfo.set_describe("gps定位成功");

            }
            else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
                curLocInfo.set_addr(location.getAddrStr());
                //运营商信息
                curLocInfo.set_operationers(location.getOperators());
                curLocInfo.set_describe("网络定位成功");
            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
                curLocInfo.set_describe("离线定位成功，离线定位结果也是有效的");
            } else if (location.getLocType() == BDLocation.TypeServerError) {
                curLocInfo.set_describe("服务端网络定位失败，可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com，会有人追查原因");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                curLocInfo.set_describe("网络不同导致定位失败，请检查网络是否通畅");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                curLocInfo.set_describe("无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机");
            }
            curLocInfo.set_locationdescribe(location.getLocationDescribe());// 位置语义化信息
            curLocInfo.set_poilist(location.getPoiList());// POI数据
            if (location.getPoiList() != null) {
                curLocInfo.set_poicount(location.getPoiList().size());
            }
            Log.i("BaiduLocationApiDem", curLocInfo.toString());
            StringBuilder sb = new StringBuilder();
            sb.append(curLocInfo.get_code()+"\n"+curLocInfo.get_describe()+"\n"+curLocInfo.get_addr()+"\n附近地点：\n");
            int i=1;
            for (Poi p:curLocInfo.get_poilist()){
                sb.append((i++)+" "+p.getName()+" "+"\n");
            }
            txtLoc.setText(sb.toString());
        }
    }
    @Override
    public void onDestroy() {
        if(myLocationClient != null && myLocationClient.isStarted()) {
            myLocationClient.stop();
            myLocationClient=null;
        }
        super.onDestroy();
    }
}
